/**
 * 登录
 */
<template>
	<view>
		<view class="flex justify-center align-center" style="width: 100%;height: 560upx;">
			<image src="../../static/image/login.jpg" mode="" style="height: 200upx;width: 460upx;margin-top: 140upx;"></image>
		</view>
		<view class="v-padding-lr-xxl">
			<form @submit="formSubmit">
				<view class="cu-form-group" style="border-bottom: 1upx solid #F0F0F0;">
					<input placeholder="输入手机号码" v-model="addform.account" name="account"></input>
				</view>
				<view class="cu-form-group" style="border-bottom: 1upx solid #F0F0F0;">
					<input placeholder="输入密码" password=true v-model="addform.password" name="password"></input>
				</view>
				<view class="margin-top-xl">
					<button style="width: 100%;" formType="submit" class="cu-btn v-bg-main lg v-font-white">登录</button>
				</view>
			</form>
			<view class="cu-form-group v-bg-content padding-0">
				<view @tap="apply" class="title" style="padding-right: 0px;">帐号申请</view>
				<view @tap="findPwd" class="title" style="padding-right: 0px;">忘记密码？</view>
			</view>
		</view>
		<view class="v-font-p v-font-center v-font-main" style="position: absolute;bottom: 70upx;width: 100%;font-size: 24upx;">
			技术支持：武汉旗天科技有限公司 4001185618
		</view>
		<v-loading :loadModal="loadModal" msg="登录中..."></v-loading>
	</view>
</template>

<script>
	import vLoading from '@/components/v-loading/v-loading.vue';
	var  graceChecker = require("@/js/formValidation/graceChecker.js");
	
	var rule = [
		{ name: "account", checkType: "notnull",  errorMsg: "手机号不能为空" },
		{ name: "password", checkType: "notnull",  errorMsg: "密码不能为空" }
	];
	export default {
		components: {
			vLoading
		},
		data() {
			return {
				loginUrl : this.$profile.url + "/console/login",
				loadModal:false,
				token:'',
				addform:{
					account:'',
					password:''
				}
			}
		},
		onLoad() {
			const that = this;
			uni.getStorage({
				key: 'token',
				success: function(res) {
					that.token = res.data;
					if(that.token != ''){
						const data = {token: that.token};
						that.submit(data);
					}
				}
			});
			uni.getStorage({
				key: 'account',
				success: function(res) {
					that.addform.account = res.data;
				}
			});
			uni.getStorage({
				key: 'password',
				success: function(res) {
					that.addform.password = res.data;
				}
			});
		},
		methods: {
			formSubmit(e){
				const that = this;
				var formData = e.detail.value;
				var checkRes = graceChecker.check(formData, rule);
				if(!checkRes){
				    uni.showToast({ title: graceChecker.error, icon: "none" });
					return;
				}
				that.submit(that.addform);
			},
			submit(data){
				const that = this;
				that.loadModal = true;
				var id = setTimeout(function(){
					that.loadModal = false;
					uni.showToast({
						icon: 'none',
						title: '网络错误，请稍后重试'
					});
					clearTimeout(id);
				},5000)
				that.$http.post(that.loginUrl,data).then(res=>{
					that.loadModal = false;
					clearTimeout(id);
					if(res.flag){
						that.$store.state.user = res.data.loginUser.user;
						that.$store.state.company = res.data.loginUser.company;
						that.$store.state.companyList = res.data.loginUser.companyList;
						that.$store.state.token = res.data.token;
						uni.setStorage({
							key: 'token',
							data: res.data.token
						});
						uni.setStorage({
							key: 'account',
							data: data.account
						});
						uni.setStorage({
							key: 'password',
							data: data.password
						});
						uni.showToast({
							icon: 'success',
							title: '登录成功'
						});
						setTimeout(function(){
							uni.reLaunch({
								url: '../index/index'
							});
						},1000)
					}else{
						uni.showToast({
							icon: 'none',
							title: res.msg
						});
					}
				})
			},
			apply(){
				uni.navigateTo({
					url:'../apply-user/apply-user'
				})
			},
			findPwd(){
				uni.navigateTo({
					url:'../reg-pwd/reg-pwd'
				})
			}
		}
	}
</script>

<style scoped>
</style>
